package com.example.cloudsentinel.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.example.cloudsentinel.common.CommonException;
import com.example.cloudsentinel.fegin.UserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 整合图形化页面配置 限流的信息
 */
@RestController
public class SpringCloudSentinelController {

    @Autowired
    private com.example.cloudsentinel.service.UserService userService;

    @Autowired
    private UserFeign userFeign;

    @GetMapping("helloWorld")
    public String helloWorld() {
        return "helloWorld";
    }


    /**
     * QPS测试
     *
     * @return
     */
    @GetMapping("QPSTest")
    public String QPSTest() {
        return "QPSTestSuccess";
    }


    /**
     * 线程数测试
     *
     * @return
     */
    @GetMapping("threadTest")
    public String threadTest() throws InterruptedException {
        Thread.sleep(2 * 1000);
        return "threadTest";
    }

    // =================================关联模式限流
    @GetMapping("wirteDB")
    public String wirteDB() throws InterruptedException {
        return "wirteDBsuccess";
    }

    @GetMapping("readDB")
    public String readDB() throws InterruptedException {
        return "readDBsuccess";
    }

    // ===================================================链路模式限流

    @GetMapping("/linkModel/test1")
    public String test1() {
        String serviceUser = userService.getUser();
        return serviceUser;
    }

    @GetMapping("/linkModel/test2")
    public String test2() {
        String userServiceUser = userService.getUser();
        return userServiceUser;
    }

    // ================================================= 预热
    @GetMapping("/warmup")
    public String testWarmUp() {
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "测试流控效果===热启动";
    }

    // ===================================================排队等待

    @GetMapping("/rateLimiter")
    public String testRateLimiter() {
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("通过了一次请求 = " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return "测试流控效果===匀速排队";
    }

    // ======================================================慢调用比例

    @RequestMapping("/slowRequestRatio")
    public String slowRequestRatio() {
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "熔断降级==慢调用比例";
    }


    // =========================================================异常比例
    private static AtomicInteger atomicInteger = new AtomicInteger(1);

    @GetMapping("abnormalProportion")
    public String abnormalProportion() throws Exception {
        int i = atomicInteger.incrementAndGet();
        if (i % 2 == 0) {
            //模拟异常和异常比率
            int a = 1 / 0;
        }
        System.out.println("通过了一次请求 = " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return "success";
    }

    // ============================================热点规则
    @GetMapping("/hotspot/{id}")
    @SentinelResource(value = "hotspot",
            blockHandlerClass = CommonException.class,
            blockHandler = "handleException",
            fallbackClass = CommonException.class,
            fallback = "fallback"
    )
    public String info(@PathVariable("id") Integer id) {
        return "成功获取数据";
    }


    // =========================================系统规则

    @RequestMapping("/systemRule")
    public String systemRule() {
        return "测试系统规则QPS";
    }

    // ============================================= 授权规则
    @GetMapping("getUserInfo")
    public String getUserInfo() {
        return userFeign.getUserInfo();
    }
}
